package com.school.repair.controller;

import com.school.repair.entity.BaoXiuDan;
import com.school.repair.entity.BaoXiuMan;
import com.school.repair.entity.RepairMan;
import com.school.repair.entity.Todo;
import com.school.repair.service.BaoXiuDanService;
import com.school.repair.service.RepairManService;
import com.school.repair.util.AjaxResult;
import com.school.repair.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.json.AbstractJackson2Decoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class RepairManController {
    @Autowired
    RepairManService repairManService;

    @Autowired
    BaoXiuDanService baoXiuDanService;

    @RequestMapping(value = "repairLogin",produces = "application/json;charset=utf-8")
    public AjaxResult login(RepairMan repairMan, HttpSession session){
        RepairMan r = repairManService.login(repairMan.getName(),repairMan.getYzm());
        if (r!=null){
            session.setAttribute("login","666");
            return AjaxResult.success(r);
        }
        return AjaxResult.error("验证码错误");
    }

    @RequestMapping(value = "repairmanAdd",produces = "application/json;charset=utf-8")
    public AjaxResult add(RepairMan repairMan){
        int r = repairManService.add(repairMan);
        if (r>0){
            return AjaxResult.success("注册成功");
        }
        return AjaxResult.error("注册失败");
    }
    @RequestMapping(value = "VerifyName",produces = "application/json;charset=utf-8")
    public AjaxResult VerifyName(@RequestParam("name")String name,@RequestParam(value = "id",defaultValue = "0")long id){
        int r = repairManService.VerifyName(name,id);
        if (r==0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    @RequestMapping(value = "repairmanUpdate",produces = "application/json;charset=utf-8")
    public AjaxResult repairmanUpdate(RepairMan repairMan){
        int r = repairManService.update(repairMan);
        if (r>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    @RequestMapping(value = "repairmanBaoxiudans",produces = "application/json;charset=utf-8")
    public Page<BaoXiuDan> repairmanBaoxiudans(@RequestParam("id")long id , @RequestParam(value = "page",defaultValue = "1")int pageNo, @RequestParam(value = "limit",defaultValue = "10")int pageSize){
       return repairManService.repairmanBaoxiudans(pageNo,pageSize,id);
    }
    @RequestMapping(value = "todosBy",produces = "application/json;charset=utf-8")
    public Page<Todo> todosBy(@RequestParam("id")long id, @RequestParam(value = "page",defaultValue = "1")int pageNo, @RequestParam(value = "limit",defaultValue = "10")int pageSize ){
       return repairManService.todosBy(id,pageNo,pageSize);
    }

}
